<template>
	<view class="user root-container">
		<view class="content">
			<view class="card">
				<view class="info" @click="toRoute('/pages-user/info')">
					<view class="flx-default">
						<uv-avatar
							:src="!token ? 'https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0826/0aeb3202508261345138408.png':userInfo.avatar"
							size="50" />
						<view class="ml10" v-if="token">
							<view class="nickname">{{userInfo.nickname}}</view>
							<view class="phone">{{userInfo.phone_number}}</view>
						</view>
						<view class="nologin-box" v-else @click.stop="onGoLogin">登录/注册</view>
					</view>
					<view class="user-lable" v-if="userInfo.role_type > 1">
						{{getRoleType(userInfo.role_type)}}
					</view>
				</view>

				<view class="row-box">
					<uv-row class="list">
						<uv-col span="6" class="plan-box">
							<view class="itemx">
								<text class="f28">邻事币</text>
								<view class="flex align-center">
									<text class="t1">0.00</text>
									<text class="t2">币</text>
								</view>
							</view>
						</uv-col>

						<uv-col span="6" class="plan-box">
							<view class="itemx2" @click="toRoute('/pages-user/coupon')">
								<text class="f28">优惠券</text>
								<view class="flex align-center">
									<text class="t1">0</text>
									<text class="t2">张未使用</text>
								</view>
							</view>
						</uv-col>
					</uv-row>
				</view>

			</view>

			<view class="shequ">
				<view class="name">
					<lst-title title="我的订单" more="" bold="bold" />
				</view>
				<view class="nav">
					<view class="grid-container">
						<!-- 服务订单 -->
						<view class="grid-item" @click="toRoute('/pages-user/order')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/027e520250816113033479.png"
								mode="aspectFit" class="icon" />
							<text class="f24">服务订单</text>
						</view>

						<!-- 邻工订单 -->
						<view class="grid-item" @click="toRoute('/pages-worker/list')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/27615202508161130562720.png"
								mode="aspectFit" class="icon" />
							<text class="f24">邻工订单</text>
						</view>

						<!-- 邻集市订单 -->
						<view class="grid-item" @click="toRoute('/pages-home/market/index?type=order')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/9214a202508161131223253.png"
								mode="aspectFit" class="icon" />
							<text class="f24">邻集市订单</text>
						</view>
					</view>
				</view>
			</view>

			
			

			<view class="shequ">
				<view class="name">
					<lst-title title="其他服务" more="" bold="bold" />
				</view>

				<view class="nav">
					<view class="grid-container1">
						<!-- 服务接单 -->
						<view class="grid-item" @click="toRoute('/pages-user/select_Identity')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/a4b48202508161131596461.png"
								mode="aspectFit" class="icon" />
							<text class="f24">服务接单</text>
						</view>

						<!-- 帮助反馈 -->
						<view class="grid-item" @click="toRoute('/pages-user/help')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/6d94d202508161127187792.png"
								mode="aspectFit" class="icon" />
							<text class="f24">帮助反馈</text>
						</view>

						<!-- 关于我们 -->
						<view class="grid-item" @click="toRoute('/pages-user/about')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/19f3a202508161127467640.png"
								mode="aspectFit" class="icon" />
							<text class="f24">关于我们</text>
						</view>

						<!-- 地址管理 -->
						<view class="grid-item" @click="toRoute('/pages-user/address')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/aad06202508161128256222.png"
								mode="aspectFit" class="icon" />
							<text class="f24">地址管理</text>
						</view>

						<!-- 设置 -->
						<view class="grid-item" @click="toRoute('/pages-user/settings')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/5ac78202508161129021836.png"
								mode="aspectFit" class="icon" />
							<text class="f24">设置</text>
						</view>

						<view class="grid-item" @click="toRoute('/pages-user/share/share')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0826/90c52202508261507443849.png"
								mode="aspectFit" class="icon" />
							<text class="f24">分享</text>
						</view>

						<view class="grid-item"
							@click="toRoute('/pages-user/training_registration/training_registration')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0828/36a17202508281025437617.png"
								mode="aspectFit" class="icon" />
							<text class="f24">培训报名</text>
						</view>

						<view class="grid-item" @click="toRoute('/pages-user/partner_reply/partner_reply')">
							<image
								src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0828/608522025082813161049.png"
								mode="aspectFit" class="icon" />
							<text class="f24">合伙人</text>
						</view>

						<!-- NFC营销入口 -->
						<view class="grid-item" @click="toRoute('/pages-nfc/index')">
							<uv-icon name="scan" size="50rpx" color="#333" class="icon" />
							<text class="f24">NFC营销</text>
						</view>
                    </view>
				</view>
			</view>

			<!-- <view class="shequ">
				<view class="name">
					<lst-title title="邻事保险" more="" bold="bold" />
				</view>
				
				<view class="nav">
					<uv-grid :border="false" col="5">
						<uv-grid-item class="item">
							<image src="https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0816/9a959202508161129271392.png" mode="aspectFit" class="icon" />
							<uv-text text="邻事保险" color="#333" size="24upx" align="center" />
						</uv-grid-item>
					</uv-grid>
				</view>
			</view> -->
			<uv-gap height="30" bgColor="transparent"></uv-gap>
		</view>
		<custom-tab-bar />
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onLoad,
		onReady,
		onShow,
		onShareAppMessage,
		onShareTimeline
	} from '@dcloudio/uni-app'

	import {
		storeToRefs
	} from 'pinia'
    import {
		useUserStore
	} from '@/store'
	import {
		getUserInfo
	} from '@/api'
	import CustomTabBar from '@/custom-tab-bar/index.vue'
	import {
		navigate
	} from '@/util/tools.js';
	const user = useUserStore()
	const {
		token,
		userInfo
	} = storeToRefs(user)

	//用户分享的id
	const user_share = ref(0)
	const toRoute = (url) => {
		if (!token.value) {
			navigate('/pages/auth/index')

		} else {
			uni.navigateTo({
				url
			})
		}
	}

	const init = async () => {
		const {
			result
		} = await getUserInfo()
		
		if(result.roles){
			//暂没用不存储
			result.roles=''
		}
		userInfo.value = result

		if (userInfo.value.id) {
			user_share.value = userInfo.value.id
			try {
				user.updateUserInfo(userInfo.value)
			} catch (error) {
				console.log('save--user-error',error)
			}
		}

	}

	const getRoleType=(role_type)=> {
		let type = role_type * 1
		let result = ''
		switch (type) {
			case 1:
				result = '消费者'
				break;
			case 2:
				result = '消费商'
				break;
			case 3:
				result = '联创合伙人'
				break;
			case 4:
				result = '权益股东'
				break;
			default:
				result = '消费者'
				break;
		}
		return result

	}

	const onGoLogin = () => {
		navigate('/pages/auth/index')
	}

	onShareAppMessage(() => {
		if (!user_share.value || user_share.value == 0) {
			return false;
		}
		return {
			title: '欢迎加入邻事通',
			path: '/pages/auth/index?source=' + user_share.value,
			imageUrl: '/static/share-cover.png'
		}
	})

	onShareTimeline(() => {
		return {
			title: '欢迎加入邻事通',
			query: 'source=' + user_share.value
		}
	})

	onShow(() => {
		token.value && init()
	})

	const ceshi = () => {
		uni.getLocation({
			type: 'gcj02', //返回可以用于uni.openLocation的经纬度
			success: function(res) {
				const latitude = res.latitude;
				const longitude = res.longitude;
				uni.openLocation({
					latitude: latitude,
					longitude: longitude,
					success: function() {
						console.log('success');
					}
				});
			}
		});
	}
</script>



<style>
	page {
		background-color: #F6F4F7;
		padding-bottom: 120upx;
		/* 给tabbar留出空间 */
	}
</style>

<style lang="scss" scoped>
	.nologin-box {
		font-size: 32upx;
		color: #fff;
		padding-left: 10upx;
	}

	.user {
		position: relative;
		padding-top: calc(var(--status-bar-height) + 100upx);

		.bg {
			z-index: 1;
			width: 100%;
			position: absolute;
			top: var(--window-top);
		}

		.content {
			z-index: 2;
			padding: 30upx;
			position: relative;
		}
	}

	.ml10 {
		margin-left: 20upx;
	}

	.mt10 {
		margin-top: 60upx;
	}

	.row-box {
		padding: 20upx;
		box-sizing: border-box;
	}

	.card {
		overflow: hidden;
		border-radius: 10upx;
		background-color: #fff;

		.info {
			display: flex;
			align-items: center;
			padding: 20upx 30upx;
			background-color: #20274B;
			justify-content: space-between;

			.nickname {
				font-size: 30upx;
				color: #fff;
			}

			.phone {
				font-size: 28upx;
				color: #C8CAC9;
				margin-top: 10upx;
			}
		}

		.list {
			padding: 30upx 20upx;
			justify-content: space-between;

			.item {
				top: 0;
				left: 0;
				width: 100%;
				padding-top: 30upx;
				padding-left: 40upx;
				position: absolute;

				.t1 {
					font-size: 28upx;
					color: #F4B66D;
					margin-right: 10upx;
				}

				.t2 {
					color: #A0A0A0;
					font-size: 28upx;
				}
			}
		}
	}

	.order {
		padding: 30upx;
		overflow: hidden;
		margin-top: 30upx;
		border-radius: 10upx;
		background-color: #fff;
	}

	.shequ {
		overflow: hidden;
		margin-top: 30upx;
		border-radius: 10upx;
		background-color: #fff;

		.name {
			padding: 30upx;
			padding-bottom: 20upx;
		}

		.nav {
			padding: 16upx;
			box-sizing: border-box;

			::v-deep .item {
				margin-bottom: 30upx;
			}
		}
	}

	/* 网格容器 */
	.grid-container {
		display: grid;
		grid-template-columns: repeat(4, 1fr);
		/* 4列等宽 */
		gap: 10px;
		/* 网格间距 */
		width: 100%;
	}

	.grid-container1 {
		display: grid;
		grid-template-columns: repeat(5, 1fr);
		/* 5列等宽 */
		gap: 10px;
		/* 网格间距 */
		width: 100%;
	}

	/* 每个网格项 */
	.grid-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding-bottom: 30upx;
	}

	/* 图标样式 */
	.icon {
		display: block;
		margin: 0 auto;
		width: 50upx;
		height: 50upx;
		margin-bottom: 10upx;
	}

	/* 文字样式 */
	.f24 {
		font-size: 24rpx;
		color: #333;
	}

	.top-plan-box {
		display: flex;
	}

	.itemx {
		padding: 20upx;
		background-image: url("https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0811/28911202508111419322025.png");
		background-repeat: no-repeat;
		background-size: 100% 100%;
		margin-right: 6upx;
		/* 强制拉伸至容器大小 */
	}

	.itemx2 {
		padding: 20upx;
		background-image: url("https://linshitong.oss-cn-shenzhen.aliyuncs.com/dev/image/2025/0811/ce2f7202508111429433674.png");
		background-repeat: no-repeat;
		background-size: 100% 100%;
		margin-left: 6upx;
		/* 强制拉伸至容器大小 */
	}

	.plan-box {
		position: relative;
		padding: 30upx 10upx;
		box-sizing: border-box;
	}

	.f28 {
		font-size: 28upx;
		color: #333;
	}

	.f24 {
		font-size: 24upx;
		color: #333;
		text-align: center;
	}

	.user-lable {
		background-color: #fdecdc;
		padding: 5upx 30upx;
		box-sizing: border-box;
		border-radius: 40upx;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 26upx;
		color: #333;
	}
</style>